package ui.patient_registration.view.patient_table;

import database.dao.DaoFactory;
import database.dao.PatientDao;
import ui.patient_registration.controller.RegistrationController;
import ui.patient_registration.models.PatientTableModel;

import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;

public class PatientTablePanel extends JPanel {
    RegistrationController presenter = RegistrationController.getInstance();

    PatientDao patientDao = new PatientDao(new DaoFactory());

    private JTable patientTable;
    private PatientTableModel patientTableModel;

    public PatientTablePanel() {
        setUpPanel();
        initComponents();
        setBackground(Color.black);
    }

    private void setUpPanel() {
        setLayout(new BorderLayout());
    }

    private void initComponents() {
        patientTableModel = new PatientTableModel(patientDao.getAllPatients());

        patientTable = new JTable(patientTableModel);
        patientTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        TableColumn tc = patientTable.getColumnModel().getColumn(2);
        tc.setCellRenderer(patientTable.getDefaultRenderer(Boolean.class));

        ListSelectionModel listSelectionModel = patientTable.getSelectionModel();
        patientTable.setSelectionModel(listSelectionModel);
        listSelectionModel.addListSelectionListener(presenter.getTableListListener());

        PatientSearchPanel patientSearchPanel = new PatientSearchPanel();
        add(patientSearchPanel, BorderLayout.NORTH);

        JScrollPane paneWithTable = new JScrollPane(patientTable);
        add(paneWithTable, BorderLayout.CENTER);

        TableBtnPanel btnPanel = new TableBtnPanel();
        add(btnPanel, BorderLayout.SOUTH);

        presenter.setPatientTableModel(patientTableModel);
        presenter.setPatientTable(patientTable);
    }
}
